// stores/permission.js
import { defineStore } from 'pinia'
import { authRoutes } from '@/router/routes'

export const usePermissionStore = defineStore('permission', {
  state: () => ({
    routes: [],
    hasGenerated: false // 保持这个状态名称
  }),

  actions: {
    // 确保generateRoutes正确设置状态
    async generateRoutes(roles) {
      try {
        if (this.hasGenerated) return
        
        console.log('正在生成路由，角色：', roles)
        // 添加过滤方法实现
        const filterRoutes = (routes) => {
          return routes.filter(route => {
            // 权限验证逻辑（需要根据实际业务补充）
            const hasPermission = !route.meta?.roles || 
                                 route.meta.roles.some(role => userRoles.includes(role))
            return !route.meta?.hidden && hasPermission
          }).map(route => ({
            ...route,
            children: route.children ? filterRoutes(route.children) : undefined
          }))
        }
        
        this.routes = filterRoutes(authRoutes)
        this.hasGenerated = true
        console.log('生成后的路由：', this.routes)
      } catch (error) {
        console.error('路由生成失败:', error)
      }
    }

  }
})
